<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html lang="en">
<head>
    <title>信息添加</title>
    <link href="https://cdn.bootcss.com/jquery-confirm/3.3.1/jquery-confirm.min.css" rel="stylesheet">

    <script type="text/javascript" charset="utf-8" src="${ctx}/resources/browser/jquery/jquery-3.1.1.js"></script>
    <script type="text/javascript" charset="utf-8" src="${ctx}/resources/browser/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="${ctx}/resources/browser/ueditor/ueditor.all.js"> </script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="${ctx}/resources/browser/ueditor/lang/zh-cn/zh-cn.js"></script>
    <script src="https://cdn.bootcss.com/jquery-confirm/3.3.1/jquery-confirm.min.js"></script>

    <style type="text/css">
        .center {
            margin:0 auto;
            width:40%
        }
    </style>
</head>
<body>
    <div class="center">
        <span>标题：</span>
        <input type="text" id="title" style="width: 40%;" />&nbsp;&nbsp;
        <span>类别：</span>
        <select id="category" style="width: 35%;">
            <option value="">-请选择-</option>
            <option value="WECHAT">微信</option>
            <option value="MAJOR">专业介绍</option>
            <option value="NEWS">系部新闻</option>
        </select>
        <br/>
        <br/>
        <span>摘要：</span>
        <textarea id="sumary" style="height:60px;width: 90%;"></textarea><br/><br/>
        <div id="editor" style="height:80%;"></div>
        <div id="message" hidden="hidden"></div>
        <div style="text-align: center">
            <button style="align-items: center" onclick="submit();">提交</button>
        </div>
    </div>
</body>
<script type="text/javascript">
    var ue = UE.getEditor('editor');

    function submit() {
        var title = $("#title").val().trim();
        if(title==null||title==undefined||title==""){
            $("#message").show().html("<h1 style='color: red'>标题不能为空<h1>");
            return;
        }
        var category = $("#category").val();
        if(category==null||category==undefined||category==""){
            $("#message").show().html("<h1 style='color: red'>类型不能为空<h1>");
            return;
        }
        var summary = $("#sumary").val().trim();
        if(summary==null||summary==undefined||summary==""){
            $("#message").show().html("<h1 style='color: red'>摘要不能为空<h1>");
            return;
        }
        var content = ue.getContent().trim();
        if(content==null||content==undefined||content==""){
            $("#message").show().html("<h1 style='color: red'>内容不能为空<h1>");
            return;
        }
        var params = {};
        params.title = title;
        params.category = category;
        params.summary = summary;
        params.content = content;
        $.ajax({
            type : "post",
            url : "${ctx}/admin/news/add",
            data : params,
            dataType: "json",
            success: function (re) {
                if (true == re.success) {
                    promptSuccess();
                } else {
                    $("#message").show().html("<h1 style='color: red'>服务器错误，请联系管理员<h1>");
                }
            },
            error: function (re) {
                $("#message").show().html("<h1 style='color: red'>网络请求失败，请检查网络或地址<h1>");
            }
        });
        function promptSuccess() {
            $.confirm({
                title: '提交成功',
                content: '继续添加?',
                type: 'green',
                icon: 'glyphicon glyphicon-question-sign',
                buttons: {
                    ok: {
                        text: '继续',
                        btnClass: 'btn-primary',
                        action: function() {
                            $("#title").val("");
                            $("#sumary").val("");
                            ue.setContent("");
                            $("#message").hide();
                        }
                    },
                    cancel: {
                        text: '关闭',
                        btnClass: 'btn-primary',
                        action: function() {
                            window.close();
                        }
                    }
                }
            });
        }
    }
</script>
</html>